package display;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;

import book.Book;


import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MBSUpdate extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField path;
	private MUActionListener al = new MUActionListener();
	private Semaphore updateSem;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			MBSUpdate dialog = new MBSUpdate();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public MBSUpdate() {
		setBounds(100, 100, 350, 115);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{50, 74, 86, 89, 0, 50, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0, 23, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		
		JLabel lblMBSFile = new JLabel("Path to MBS DB");
		GridBagConstraints gbc_lblMBSFile = new GridBagConstraints();
		gbc_lblMBSFile.anchor = GridBagConstraints.WEST;
		gbc_lblMBSFile.insets = new Insets(0, 0, 5, 5);
		gbc_lblMBSFile.gridx = 1;
		gbc_lblMBSFile.gridy = 2;
		contentPanel.add(lblMBSFile, gbc_lblMBSFile);
	
		path = new JTextField();
		path.setPreferredSize(new Dimension(100, 20));
		GridBagConstraints gbc_path = new GridBagConstraints();
		gbc_path.fill = GridBagConstraints.HORIZONTAL;
		gbc_path.gridwidth = 2;
		gbc_path.insets = new Insets(0, 0, 5, 5);
		gbc_path.gridx = 2;
		gbc_path.gridy = 2;
		contentPanel.add(path, gbc_path);
		path.setColumns(10);
	
		JButton btnFileChooser = new JButton("...");
		btnFileChooser.addActionListener(al);
		btnFileChooser.setActionCommand("fchoose");
		GridBagConstraints gbc_btnFileChooser = new GridBagConstraints();
		gbc_btnFileChooser.insets = new Insets(0, 0, 5, 5);
		gbc_btnFileChooser.anchor = GridBagConstraints.NORTHWEST;
		gbc_btnFileChooser.gridx = 4;
		gbc_btnFileChooser.gridy = 2;
		contentPanel.add(btnFileChooser, gbc_btnFileChooser);
	
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		
		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.addActionListener(al);
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
	
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(al);
		buttonPane.add(cancelButton);
			
		}
	
	private class MUActionListener implements ActionListener {
		
		private File[] fs = null;
		
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getActionCommand().equals("fchoose"))
				fileChooser();
			else if (arg0.getActionCommand().equals("OK"))
				update();
			else if (arg0.getActionCommand().equals("Cancel"))
				MBSUpdate.this.dispose();
		}
		
		private void update() {
			if (fs != null) {
				updateSem = new Semaphore(1-fs.length);
				new Runnable() {
					public void run() {
						for (File f : fs)
							new MBSThread(f,updateSem).start();
					}
					
				}.run();
			}
			MBSUpdate.this.dispose();
		}
		
		private void fileChooser() {

			JFileChooser jfc = new JFileChooser(new File("c:\\converted"));
			jfc.setMultiSelectionEnabled(true);
			jfc.setFileFilter(new FileFilter(){

				private String ext = ".csv";
				@Override
				public boolean accept(File arg0) {
					if (arg0.isDirectory())
						return true;
					else if (arg0.getName().toLowerCase().endsWith(ext))
						return true;
					else
						return false;
				}
				@Override
				public String getDescription() {
					return "files ending in "+ext;
				}
				
			});
			int fileFlag = jfc.showOpenDialog(MBSUpdate.this);
			
			if (fileFlag == JFileChooser.APPROVE_OPTION) {
				fs = jfc.getSelectedFiles();
				String p = fs[0].getAbsolutePath();
				
				for (int i = 1; i < fs.length; ++i)
					p += ", "+fs[i].getName();
				path.setText(p);
			}
		}
	}
}
